//
// File:	SoundManager.cpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Created:	28/04/2010 at 03:10:04
// Purpose:	
//
#include "Base.hpp"
#include "Sound.hpp"
#include "SoundManager.hpp"

//------------------------------------------------------------------
// Constructor / Destructor
//------------------------------------------------------------------
SoundManager::SoundManager()
	:	m_soundsList()
{
}

SoundManager::~SoundManager()
{
}

//------------------------------------------------------------------
// Create Sound
//------------------------------------------------------------------
ISound *SoundManager::CreateSound(const std::string &filename)
{
	ISound *pSound = new Sound();
	if (!pSound->LoadFromFile(filename))
	{
		delete pSound;
		return NULL;
	}
	m_soundsList.push_back(pSound);
	return pSound;
}

//------------------------------------------------------------------
// Update
//------------------------------------------------------------------
void SoundManager::Update()
{
	MuteSounds();
	UpdateSounds();
}

//------------------------------------------------------------------
// Mute Sounds
//------------------------------------------------------------------
void SoundManager::MuteSounds()
{
	tSoundList::iterator it = m_soundsList.begin();
	tSoundList::iterator ite = m_soundsList.end();
	for (; it != ite; ++it)
	{
		ISound *pSound = *it;

		//Should we mute or unmute the sounds ?
		void (ISound::*pfnFunc)() = &ISound::UnMute;
		if (!GlobalVariables::DisplayManager::Get().IsActive())
			pfnFunc = &ISound::Mute;
		if (pSound->IsSpatializable())
			if (!GlobalVariables::DisplayManager::Get().GetView()->IsPointInside(pSound->GetX(), pSound->GetY(), 1.25f))
				pfnFunc = &ISound::Mute;
		(pSound->*pfnFunc)();
	}
}

//------------------------------------------------------------------
// Update Sounds
//------------------------------------------------------------------
void SoundManager::UpdateSounds()
{
	tSoundList::iterator it = m_soundsList.begin();
	tSoundList::iterator ite = m_soundsList.end();
	for (; it != ite; ++it)
	{
		ISound *pSound = *it;
		if (pSound != NULL)
		{
			pSound->Update();
		}
	}
}

//------------------------------------------------------------------
// Remove Deleted Sounds
//------------------------------------------------------------------
void SoundManager::RemoveDeletedSounds()
{
	tSoundList::iterator it = m_soundsList.begin();
	tSoundList::iterator ite = m_soundsList.end();
	while (it != ite)
	{
		ISound *pSound = *it;
		if (!pSound || pSound->IsRemoved())
		{
			delete pSound;
			it = m_soundsList.erase(it);
			continue;
		}
		++it;
	}
}

//------------------------------------------------------------------
// Clear All Sounds
//------------------------------------------------------------------
void SoundManager::ClearAllSounds()
{
	tSoundList::iterator it = m_soundsList.begin();
	tSoundList::iterator ite = m_soundsList.end();
	for (; it != ite; ++it)
	{
		delete *it;
	}
	m_soundsList.clear();
}
